/**
 * 
 */
package org.vsg.core.cache.memcached;

import java.io.Serializable;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import net.spy.memcached.MemcachedClient;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.vsg.core.cache.CacheService;

/**
 * @author Bill vison
 *
 */
public class SimpleCacheServiceImpl implements CacheService , InitializingBean {

	private Logger logger = LoggerFactory.getLogger(SimpleCacheServiceImpl.class);
	
	// ---- set the memory client -------
	private MemcachedClient memClient = null;
	
	private String host = "localhost";
	
	private int port = 11211;
	
	private int liveTimeOut = 60;
	
	private int overTimeOut = 5;
	
	
	/* (non-Javadoc)
	 * @see org.vsg.core.cache.CacheService#putInCache(java.io.Serializable, java.lang.Object)
	 */
	public void put(Serializable key, Object value) {
		// TODO Auto-generated method stub
		
		Future<Boolean> future =  memClient.set( key.toString(), liveTimeOut, value);
		
		logger.debug("add to memory cache : " + key.toString() + " , " + future );

	}


	public Object get(Serializable key) {
		// TODO Auto-generated method stub
	
		Object resultObj = null;
		Future<Object> future =  memClient.asyncGet( key.toString() );
		try {
			resultObj = future.get( overTimeOut , TimeUnit.SECONDS );
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			future.cancel(true);
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			future.cancel(true);
		} catch (TimeoutException e) {
			// TODO Auto-generated catch block
			future.cancel(true);
		}
		
		return resultObj;
	}
	

	

	public boolean remove(Serializable key) {
		// TODO Auto-generated method stub
		Future retu =(Future<Boolean>) memClient.delete(key.toString()) ;
		Boolean result = false;
		try {
			result = (Boolean)retu.get();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	public void afterPropertiesSet() throws Exception {
		// TODO Auto-generated method stub
		InetSocketAddress socketAddress = new InetSocketAddress(host , port);
		
		memClient = new MemcachedClient( socketAddress );
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public MemcachedClient getMemClient() {
		return memClient;
	}

	public void setMemClient(MemcachedClient memClient) {
		this.memClient = memClient;
	}

	public int getLiveTimeOut() {
		return liveTimeOut;
	}

	public void setLiveTimeOut(int liveTimeOut) {
		this.liveTimeOut = liveTimeOut;
	}

	
	
	
}
